/*
 * Copyright (C) 2012 Jesse Webb (gweebz@gmail.com)
 *
 * This file is part of Gwahtzee.
 *
 * Gwahtzee is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Gwahtzee is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Gwahtzee.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.googlecode.gwahtzee.gui.mainmenu;

import org.newdawn.slick.*;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;

public class MainMenuState extends BasicGameState {

    private static final int LOGO_X = 280;
    private static final int LOGO_Y = 100;
    private static final int MENU_X = 280;
    private static final int MENU_Y = 200;
    //    private static final int MENU_BUTTON_WIDTH = 500;
    private static final int MENU_BUTTON_HEIGHT = 60;
    private static final int MENU_BUTTON_SEPARATOR = 5;

    private Image backgroundImage;
    private Image logoImage;
    private Menu menu;

    private int stateId;

    public MainMenuState(int stateId) {
        this.stateId = stateId;
    }

    @Override
    public int getID() {
        return stateId;
    }

    @Override
    public void init(final GameContainer gameContainer, final StateBasedGame stateBasedGame) throws SlickException {
        backgroundImage = new Image("images/background.png");
        logoImage = new Image("images/gwahtzee-logo.png");

        menu = new Menu();

        Button newGameButton = new Button(MENU_X, MENU_Y, new Image("images/new-game.png")) {
            @Override
            public void click() {
                stateBasedGame.enterState(com.googlecode.gwahtzee.gui.Game.PLAY_GAME_STATE_ID, new FadeOutTransition(), new FadeInTransition());
            }
        };
        menu.addButton(newGameButton);

        Button highScoresButton = new Button(MENU_X, MENU_Y + MENU_BUTTON_HEIGHT + MENU_BUTTON_SEPARATOR, new Image("images/high-scores.png")) {
            @Override
            public void click() {
                // TODO - enter high scores state
            }
        };
        menu.addButton(highScoresButton);

        Button optionsButton = new Button(MENU_X, MENU_Y + (2 * (MENU_BUTTON_HEIGHT + MENU_BUTTON_SEPARATOR)), new Image("images/options.png")) {
            @Override
            public void click() {
                // TODO - enter options state
            }
        };
        menu.addButton(optionsButton);

        Button exitButton = new Button(MENU_X, MENU_Y + (3 * (MENU_BUTTON_HEIGHT + MENU_BUTTON_SEPARATOR)), new Image("images/exit.png")) {
            @Override
            public void click() {
                gameContainer.exit();
            }
        };
        menu.addButton(exitButton);
    }

    @Override
    public void update(GameContainer gameContainer, StateBasedGame stateBasedGame, int delta) throws SlickException {
        Input input = gameContainer.getInput();
        int mouseX = input.getMouseX();
        int mouseY = input.getMouseY();

        if (input.isKeyPressed(Input.KEY_ESCAPE)) gameContainer.exit();

        menu.update(input, mouseX, mouseY, delta);
    }

    @Override
    public void render(GameContainer gameContainer, StateBasedGame stateBasedGame, Graphics graphics) throws SlickException {
        backgroundImage.draw(0, 0);
        logoImage.draw(LOGO_X, LOGO_Y);
        menu.render();
    }
}
